package com.mall507.fivepeaks.activity.main;

import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.activity.mine.MessageRFM;
import com.mall507.fivepeaks.widget.Banner.BGABanner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/main/MainTownShipInfoUI")
public class MainTownShipInfoUI extends BaseUI{
    @ViewInject(R.id.tablayout)
    TabLayout mTabLayout;
    @ViewInject(R.id.viewPager)
    ViewPager mViewPager;
    @ViewInject(R.id.head_banner)
    BGABanner banner;
    

    String[] stateName = new String[]{"乡镇介绍", "乡镇动态"};
    int[] stateValue = new int[]{1, 2};
    MyPagerAdapter myPagerAdapter;
    private List<String> listUrl;
    
    @Override
    protected void onCreateBase() {
        setContentView(R.layout.ui_main_township_info);
        showLoding();
        initViewPager(); // 初始化ViewPager
        mViewPager.setOffscreenPageLimit(3);
        getComment();
    }

    private void initViewPager() {
        // 创建一个集合,装填Fragment
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(MainTownShipJieShao1FM.newInstance(stateValue[0],getIntent().getStringExtra("id")));
        fragments.add(MainTownShipDongTaiRFM.newInstance(stateValue[1],getIntent().getStringExtra("id")));
        // 创建ViewPager适配器
        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        myPagerAdapter.setFragments(fragments);
        // 给ViewPager设置适配器
        mViewPager.setAdapter(myPagerAdapter);
        // TabLayout 指示器 (记得自己手动创建4个Fragment,注意是 app包下的Fragment 还是 V4包下的 Fragment)
        for (int i = 0; i < stateValue.length; i++) {
            mTabLayout.addTab(mTabLayout.newTab());
        }
        // 使用 TabLayout 和 ViewPager 相关联
        mTabLayout.setupWithViewPager(mViewPager);
        // TabLayout指示器添加文本
        for (int i = 0; i < stateName.length; i++) {
            mTabLayout.getTabAt(i).setText(stateName[i]);
        }
        //tab的字体选择器,默认黑色,选择时红色
        mTabLayout.setTabTextColors(ContextCompat.getColor(this, R.color.base_666666), ContextCompat.getColor(this, R.color.application_color));
        //tab的下划线颜色,默认是粉红色
        mTabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.application_color));
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        //当前Fragment
        private List<Fragment> mFragmentList;

        @Override
        //每次Fragment切换都会调用到FragmentPagerAdapter.setPrimaryItem 方法
        public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            super.setPrimaryItem(container, position, object);
        }

        public void setFragments(ArrayList<Fragment> fragments) {
            mFragmentList = fragments;
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            Fragment fragment = mFragmentList.get(position);

            return fragment;
        }

        @Override
        public int getCount() {

            return mFragmentList.size();
        }
    }

    private void getComment() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BaseHttp.getInstance().query("Township/getTownshipOne", params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (200 == response.optInt("code")){
                    setView();
                    JSONObject data = response.optJSONObject("data");
                    String title = data.optString("township_name");
                    getTitleView().setContent(title.length()>10?title.substring(0,9) + "...":title);
                    JSONArray arrayIcons = data.optJSONArray("round_img");
                    if (arrayIcons != null && arrayIcons.length() > 0) {
                        listUrl = new ArrayList<>();
                        for (int i = 0; i < arrayIcons.length(); i++) {
                            listUrl.add(arrayIcons.optString(i));
                        }
                    }
                    bindTopBannerData(listUrl);
                }
                dismissLoding();
            }
        });
    }

    //banner数据绑定
    private void bindTopBannerData(List<String> listUrl) {
        banner.setAdapter(new BGABanner.Adapter<View, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, View itemView, String url, int position) {
                SimpleDraweeView simpleDraweeView = itemView.findViewById(R.id.sdv_item_fresco_content);
                simpleDraweeView.setImageURI(url);
            }
        });
        banner.setData(R.layout.homerecycle_top_banner_content, listUrl, null);
    }
    
    private void setView(){
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
            params.put("type", getIntent().getStringExtra("type"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BaseHttp.getInstance().query("Action/viewBlog", params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
            }
        });
    }
}
